package com.efounder.utils;

import android.view.View;
import android.view.WindowInsets;

/**
 * ApplyWindowInsetsUtil工具类
 * Created by yqs97.
 * Date: 2020/1/16
 * Time: 14:25
 */
public class ApplyWindowInsetsUtil {

    /**
     * 解决沉浸式状态栏使用fitswindows解决输入法无法弹出后布局多一块padiing的问题
     * https://www.jianshu.com/p/aca4fd6743b1
     * @param view 含有fitswindows属性的view
     */
    public static void setApplyWindowInsets(View view) {
        try {
            //接管该View的Windows Insets应用策略 https://www.jianshu.com/p/5cc3bd23be7b
            view.setOnApplyWindowInsetsListener(new View.OnApplyWindowInsetsListener() {
                @Override
                public WindowInsets onApplyWindowInsets(View view, WindowInsets insets) {
                    //根布局应用底边inset
                    WindowInsets newInsets = insets.replaceSystemWindowInsets(0, 0, 0, insets.getSystemWindowInsetBottom());
                    view.onApplyWindowInsets(newInsets);
                    //子布局应用顶边inset
                    return insets.replaceSystemWindowInsets(0, insets.getSystemWindowInsetTop(), 0, 0);
                }
            });
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

}
